package com.example.dream.excel.demo.controller;

/**
 * @className EasyExcelController
 * @Description TODO
 * @Autor WUAO
 * @Date 2022/11/21 10 19
 */

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.dream.excel.demo.model.UserExcel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
     /**
      *  使用EasyExcel操作excel文件上传/下载
        */
@Controller
@RequestMapping(value = "/xlsx")
public class EasyExcelController {

    @RequestMapping("/toExcelPage")
    public String todownloadPage() {
        return "excelPage";
    }

    /**
     * 下载Excel
     * @param request
     * @param response
     */
    @RequestMapping(value = "/downloadExcel")
    public void downloadExcel(HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 设置防止中文名乱码
        String filename = URLEncoder.encode("员工信息", "utf-8");
        // 文件下载方式(附件下载还是在当前浏览器打开)
        response.setHeader("Content-disposition", "attachment;filename=" +
                filename + ".xlsx");
        // 构建写入到excel文件的数据
        List<UserExcel> userExcels = new ArrayList<>();
        UserExcel userExce1 = new UserExcel(1001, "张三", "男", 1333.33,
                new Date());
        UserExcel userExce2 = new UserExcel(1002, "李四", "男", 1356.83,
                new Date());
        UserExcel userExce3 = new UserExcel(1003, "王五", "男", 1883.66,
                new Date());
        UserExcel userExce4 = new UserExcel(1004, "赵六", "男", 1393.39,
                new Date());
        userExcels.add(userExce1);
        userExcels.add(userExce2);
        userExcels.add(userExce3);
        userExcels.add(userExce4);
        // 写入数据到excel
        EasyExcel.write(response.getOutputStream(), UserExcel.class)
                .sheet("用户信息")
                .doWrite(userExcels);
    }



         @RequestMapping("/uploadExcel")
         @ResponseBody
         public String uploadExcel(@RequestParam("file") Part part,HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
             System.out.println(1111111);
             List<UserExcel> list=new ArrayList<UserExcel>();
             // 获取上传的文件流
             InputStream inputStream = part.getInputStream();
             // 读取Excel
             EasyExcel.read(inputStream, UserExcel.class,
                     new AnalysisEventListener<UserExcel>() {
                         // 每解析一行数据,该方法会被调用一次
                         @Override
                         public void invoke(UserExcel data, AnalysisContext analysisContext) {
                             System.out.println("解析数据为:" + data.toString());
                             list.add(data);
                         }

                         // 全部解析完成被调用
                         @Override
                         public void doAfterAllAnalysed(AnalysisContext analysisContext) {
                             System.out.println("解析完成...");
                             // 可以将解析的数据保存到数据库
                         }
                     }).sheet().doRead();

             System.out.println("获取数组队列对象");
             for (int i = 0; i < list.size(); i++) {

                 System.out.println(list.get(i).getUsername());
             }
             // 设置响应头
             response.setContentType("application/vnd.ms-excel");
             response.setCharacterEncoding("utf-8");
             // 设置防止中文名乱码
             String filename = URLEncoder.encode("测试结果", "utf-8");
             // 文件下载方式(附件下载还是在当前浏览器打开)
             response.setHeader("Content-disposition", "attachment;filename=" +
                     filename + ".xlsx");
             EasyExcel.write(response.getOutputStream(), UserExcel.class)
                     .sheet("用户信息")
                     .doWrite(list);
            return "上传成功";
         }

     }

